﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;
using System.Web.Script.Serialization;
using DataAccessEntLib;
using System.Text;
using System.Linq;

public partial class Notes_NTS_DocDetails : System.Web.UI.Page
{

    BusinessLogic busin = new BusinessLogic();
    BO.Ad bo = new BO.Ad(); 
    RegistrationData _objRegistrationData = new RegistrationData();
    Notes.NotesDB _objNotesDB = new Notes.NotesDB();
    protected int ImageLimit = 0, VideoLimit = 0, CurrentUploadImages = 0, DocID = 0;
    string ImageName = string.Empty, folderPath = string.Empty, ImagePath = string.Empty;
    public string Url = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.QueryString["DocID"] != null)
        {
            if (!IsPostBack)
            {
                if (int.TryParse(Request.QueryString["DocID"], out DocID))
                {
                    BindDocDetails(DocID);
                }
            }
        }
        else
        {
            Response.Redirect(Common.LogOutPage);
        }
    }


    protected void BindDocDetails(int DocID,DataSet ds=null)
    {
        if (DocID > 0)
        {
            try
            {
                _objNotesDB.DocumentID = DocID;
                if (ds == null)
                  ds = _objNotesDB.Select_DocsMaster(DocID, "DocsDetails");
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            DataRow dr = ds.Tables[0].Rows[0];
                            lblDocTitle.Text = Convert.ToString(dr["Title"]);
                            lblItemViews.Text = Convert.ToString(dr["TotalDownloads"]);
                            lblSubjectName.Text = Convert.ToString(dr["SubjectName"]);
                            lblStream.Text = Convert.ToString(dr["Stream"]);
                            lblUniversity.Text = Convert.ToString(dr["UniversityName"]);
                            lblDesc.Text = Convert.ToString(dr["Description"]);
                            lblDocType.Text = Convert.ToString(dr["DocumentType"]);
                            if (!string.IsNullOrEmpty(Convert.ToString(dr["FileName"])))
                            {
                                lblDocName.Text = Convert.ToString(dr["FileName"]);
                                if (lblDocName.Text.Contains('.'))
                                {
                                    var fileType = lblDocName.Text.Split('.').LastOrDefault();
                                    imgIcon.Attributes.Add("class", fileType);
                                }
                            }
                        }
                    }
                }
                ds.Dispose();
                 
            }
            catch (Exception ex)
            {
                //lblMessage.Text = ex.Message;
            }

        }
    }

    public void DownloadFile()
    {
        if (int.TryParse(Request.QueryString["DocID"], out DocID))
        {
            DataSet ds = _objNotesDB.Select_DocsMaster(DocID, "DocsDetails",0,0,0,0,true);
            byte[] file;
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DataRow dr=ds.Tables[0].Rows[0];
                        file = (byte[])dr["DocData"];

                        HttpContext.Current.Response.ContentType =Convert.ToString( dr["Extenstion"]);
                        HttpContext.Current.Response.OutputStream.Write(file, 0, Convert.ToInt32(dr["DocLength"]));
                        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Convert.ToString(dr["FileName"]));
                        HttpContext.Current.Response.End();
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "mykey", "<script language=\"javascript\"type=\"text/javascript\">window.close();</script>");
                    }
                }
            }
        }
    }

    //protected void BindSimilarAds(int DocID)
    //{
    //    if (DocID > 0)
    //    {
    //        DataSet ds = new DataSet();
    //        try
    //        {
    //            bo.DocID = DocID;
    //            ds = bll.Select_RelatedAds(bo);
    //            if (!object.Equals(ds, null))
    //            {
    //                if (ds.Tables.Count > 0)
    //                {
    //                    StringBuilder sb = new StringBuilder();
    //                    int looplengh = 0;
    //                    if (ds.Tables[0].Rows.Count < 5)
    //                        looplengh = ds.Tables[0].Rows.Count;
    //                    else
    //                        looplengh = 5;

    //                    for (int i = 0; i < looplengh; i++)
    //                    {
    //                        DataRow dr = ds.Tables[0].Rows[i];
    //                        if (!string.IsNullOrEmpty(Convert.ToString(dr["ImagePath"])))
    //                        {
    //                            sb.Append("<li><a href='Ad-Details.aspx?DocID=" + Convert.ToString(dr["DocID"]) + "'><div class='porduct_img'><img src='" + Convert.ToString(dr["ImagePath"]) + "' border='0'>");
    //                            sb.Append("</div><p>" + Convert.ToString(dr["AdTitle"]) + "</p></a></li>");
    //                        }
    //                        else
    //                        {
    //                            sb.Append("<li><a href='Ad-Details.aspx?DocID=" + Convert.ToString(dr["DocID"]) + "'><div class='porduct_img'><img src='images/no-image.jpg' border='0'>");
    //                            sb.Append("</div><p>" + Convert.ToString(dr["AdTitle"]) + "</p></a></li>");

    //                        }

    //                    }
    //                    ulRelated.InnerHtml = sb.ToString();
    //                }
    //            }
    //            ds.Dispose();
    //        }
    //        catch (Exception ex)
    //        {
    //            //lblMessage.Text = ex.Message;
    //        }

    //    }
    //}
    protected void lnkDownload_Click(object sender, EventArgs e)
    {
        DownloadFile();
    }

}